package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Aluno;
import br.com.edumobi.utils.Mask;

public class ContatoAlunoFragment extends GenericoFragment {

    private TextView textViewNomeTelaContatoAlunoFragment;
    private EditText editTextTelefoneResidencialAlunoFragment;
    private EditText editTextTelefoneCelularAlunoFragment;
    private Button buttonSalvarContatoAlunoFragment;
    private ProgressBar progressBarContatoAlunoFragment;

    private ContatoAlunoTask task;
    private AcoesContatoAlunoFragment activity;
    private Aluno aluno;

    View.OnClickListener clickButtonSalvarContatoAlunoFragment = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            String telefoneResidencial = editTextTelefoneResidencialAlunoFragment.getText().toString();
            String telefoneCelular = editTextTelefoneCelularAlunoFragment.getText().toString();

            if(activity.conectado()){
                aluno.telefoneResidencial = telefoneResidencial;
                aluno.telefoneCelular = telefoneCelular;

                if (task == null || task.getStatus().equals(AsyncTask.Status.FINISHED)) {
                    task = new ContatoAlunoTask();
                    task.execute();
                }
            }else{
                activity.falarEExibirTexto(R.string.excecao_sem_conexao_internet);
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesContatoAlunoFragment) getActivity();

        aluno = activity.obterAluno();

        editTextTelefoneResidencialAlunoFragment.setText(aluno.telefoneResidencial);
        editTextTelefoneCelularAlunoFragment.setText(aluno.telefoneCelular);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contato_aluno, container, false);

        textViewNomeTelaContatoAlunoFragment = (TextView) view.findViewById(R.id.textViewNomeTelaContatoAlunoFragment);
        editTextTelefoneResidencialAlunoFragment = (EditText) view.findViewById(R.id.editTextTelefoneResidencialAlunoFragment);
        editTextTelefoneCelularAlunoFragment = (EditText) view.findViewById(R.id.editTextTelefoneCelularAlunoFragment);
        buttonSalvarContatoAlunoFragment = (Button) view.findViewById(R.id.buttonSalvarContatoAlunoFragment);
        progressBarContatoAlunoFragment = (ProgressBar) view.findViewById(R.id.progressBarContatoAlunoFragment);

        editTextTelefoneResidencialAlunoFragment.addTextChangedListener(Mask.insert("(##) #### ####", editTextTelefoneResidencialAlunoFragment));
        editTextTelefoneCelularAlunoFragment.addTextChangedListener(Mask.insert("(##) #### ####", editTextTelefoneCelularAlunoFragment));
        buttonSalvarContatoAlunoFragment.setOnClickListener(clickButtonSalvarContatoAlunoFragment);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            esconderComponentes();
            progressBarContatoAlunoFragment.setVisibility(View.VISIBLE);
        }

        return view;
    }
    @Override
    public void exibirComponentes() {
        textViewNomeTelaContatoAlunoFragment.setVisibility(View.VISIBLE);
        editTextTelefoneResidencialAlunoFragment.setVisibility(View.VISIBLE);
        editTextTelefoneCelularAlunoFragment.setVisibility(View.VISIBLE);
        buttonSalvarContatoAlunoFragment.setVisibility(View.VISIBLE);
        progressBarContatoAlunoFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewNomeTelaContatoAlunoFragment.setVisibility(View.GONE);
        editTextTelefoneResidencialAlunoFragment.setVisibility(View.GONE);
        editTextTelefoneCelularAlunoFragment.setVisibility(View.GONE);
        buttonSalvarContatoAlunoFragment.setVisibility(View.GONE);
        progressBarContatoAlunoFragment.setVisibility(View.GONE);
    }

    private class ContatoAlunoTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarContatoAlunoFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            return activity.salvarDadosAluno(aluno);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            exibirComponentes();
            progressBarContatoAlunoFragment.setVisibility(View.GONE);

            if (aBoolean) {
                activity.falarEExibirTexto(R.string.dados_salvos_sucesso);
                activity.salvarPreferencesAluno(aluno);
            } else {
                activity.falarEExibirTexto(R.string.excecao_atualizacao_dados_aluno_malsucedida);
            }
        }
    }

    public interface AcoesContatoAlunoFragment extends AcoesGenericoFragment{
        Aluno obterAluno();
        boolean salvarDadosAluno(Aluno aluno);
    }
}